﻿using System;
using System.Collections.Generic;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class HocHamBiz
    {
        #region Properties

        private IHocHamDao _hocHamDao;
        private IHocHamDao HocHamData
        {
            get { return _hocHamDao ?? (_hocHamDao = new HocHamDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region Methods

        public List<HocHam> GetAllHocHams()
        {
            return HocHamData.GetAll();
        }

        public object GetHocHams(int startIndex, int pageSize, string sorting, string name)
        {
            try
            {
                int totalRecords;
                var hocHams = HocHamData.GetHocHams(startIndex, pageSize, sorting, name, out totalRecords);
                return new { Result = "OK", Records = hocHams, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public HocHam CreateHocHam(HocHam hocHam)
        {
            // validate inputs
            if (hocHam == null) return null;

            return string.IsNullOrEmpty(hocHam.Ten) ? null : HocHamData.AddHocHam(hocHam);
        }

        public HocHam GetById(int id)
        {
            return id > 0 ? HocHamData.GetById(id) : null;
        }

        public void UpdateHocHam(HocHam hocHam)
        {
            if (hocHam == null) return;

            HocHamData.UpdateHocHam(hocHam);
        }

        public object DeleteHocHam(int id)
        {
            try
            {
                HocHamData.DeleteHocHam(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
